from appium import webdriver
import time

import os
import json
from selenium.webdriver.common.by import By


class UtilsAppDriver:
    _driver = None

    @classmethod
    def get_app_driver(cls):
        """
        获取对象驱动方法
        :return: 驱动对象
        """
        if cls._driver == None:
            desired_caps = {
                'platformName': 'Android',
                'platformVersion': '11.0',
                'deviceName': 'ZY22C9L8KK',
                'appPackage': 'in.hirect',
                'appActivity': '.login.activity.SplashActivity',
                'resetKeyBoard': True,
                'unicodeKeyboard': True,
                'noReset': True,
                'automationName': 'Uiautomator2'
            }
            # desired_caps = {
            #     "app": "/Users/cwp/Library/Developer/Xcode/DerivedData/WangYe-dujbxzsmstdpukfpnbglzvirnzvx/Build/Products/Debug-iphoneos/WangYe.app",
            #     "platformName": "iOS",
            #     "platformVersion": "14.0",
            #     "deviceName": "iPhone",
            #     "bundleId": "com.cvte.appiumSimpleDemocwp",
            #     "udid": "9426438865055b07bba7a13b84742cb072a5759a",
            #     "xcodeSigningId": "iPhone Developer",
            #     "xcodeOrgId": "42M9APZZ4V",
            #     "automationName": "XCUITest",
            #     "noReset": True,
            #     "useNewWDA": True
            # }

            cls._driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        return cls._driver

    @classmethod
    def quit_app_driver(cls):
        """
        退出驱动对象方法
        """
        if cls._driver is not None:
            cls.get_app_driver().quit()
            cls._driver = None


def parse_login_data(file_name):
    """
    解析登录脚本数据文件
    :param file_name: login_data.json 具体路径
    :return:
    """
    with open(file_name, encoding='utf-8') as file:
        data_list = json.load(file)
        temp_list = []
        for data_dic in data_list:
            phone_number = data_dic["phone_number"]
            password = data_dic["password"]
            expect_msg = data_dic["expect"]
            temp_list.append((phone_number, password, expect_msg))
    return temp_list


if __name__ == '__main__':
    base_path = os.path.dirname(os.path.abspath(__file__))
    file_path = base_path + '/data/login_data.json'
    print(parse_login_data(file_path))
